package mutation;


import ga.Individual;
import java.util.List;
import operations.Operation;
import operations.ValuesGenerator;
import util.DebugConfig;
import util.Log;


public class DeleteRandomMethodCall extends MutationStrategy {

	@Override
	public Individual mutate(Individual individual) {
		if(DebugConfig.deleteRandomMethodClass){
			Log.log("-------  Delete Random Method Call Mutation ---------------"); 
			Log.log("--------------------individual1----------------------");
			Log.log(individual.toString()+ " size:"+ individual.getSize());
		}

		Individual mutatedIndividual=individual.clone();
		List<Operation> ops =mutatedIndividual.getStatements();
		try {
			if(ops.size()<3)return individual;
			int numOfMethods=ValuesGenerator.getRandomIntBetween(3, ops.size());
			for(int i=1;i<=numOfMethods && ops.size()>3;i++){
				int randPos = ValuesGenerator.getRandomIntBetween(0, ops.size()-1);
				ops.remove(randPos);
			}
			if(DebugConfig.deleteRandomMethodClass){
				Log.log("--------------------individual1----------------------");
				Log.log(individual.toString()+ " size:"+ individual.getSize());
				Log.log("--------------------mutatedIndividual----------------------");
				Log.log(mutatedIndividual.toString()+ " size:"+ mutatedIndividual.getSize());
			}

			return mutatedIndividual;
		} catch (Throwable e){
			System.err.println("Delete Random Method Call Class:" + e.getMessage());
		}	
		return null;
		
	}

	@Override
	public boolean getMutationFlag(int individualSize) {
		return true;
	}
}

